package cn.zyb.system.controller;

import cn.zyb.basic.util.AjaxResult;
import cn.zyb.basic.util.PageList;
import cn.zyb.system.domain.Sdt;
import cn.zyb.system.query.SdtQuery;
import cn.zyb.system.service.ISdtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 接口类：类型
 */
@RestController
@RequestMapping("/sdt")
@Api(value = "类型接口类",description = "类型接口的CRUD操作")
public class SdtController {
    @Autowired
    private ISdtService sdtService;

    /**
     * 接口：查询所有
     * @return
     */
    // http://localhost:8080/sdt - Get
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Sdt> findAll(){
        return sdtService.findAll();
    }

    /**
     * 接口：查询单个对象
     * @return
     */
    // http://localhost:8080/sdt/2 - Get
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Sdt findOne(@PathVariable("id") Long id){
        return sdtService.findOne(id);
    }

    /**
     * 分页接口
     * @param sdtQuery
     * @return
     */
    // http://localhost:8080/sdt - Post - 参数通过json对象传递
    @PostMapping
    //@RequestBody -从请求体中获取数据赋值给sdtQuery
    @ApiOperation(value = "分页接口")
    public PageList<Sdt> queryPage(@RequestBody SdtQuery sdtQuery){
        return sdtService.queryPage(sdtQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    // http://localhost:8080/sdt/2 - Delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            sdtService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
//            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    /**
     * 接口：添加和修改
      * @param sdt
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口")
    public AjaxResult addOrUpdate(@RequestBody Sdt sdt){
        try {
            if(sdt.getId()==null){//添加
                sdtService.add(sdt);
            }else{
                sdtService.update(sdt);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

  /**
   * 接口：批量删除
   * @param ids
   * @return
   */
  @PatchMapping
  @ApiOperation(value = "批量删除接口")
  public AjaxResult patchDel(@RequestBody List<Long> ids){
    try {
      sdtService.patchDel(ids);
      return new AjaxResult();
    } catch (Exception e) {
      e.printStackTrace();
      return new AjaxResult(false,"删除失败");
    }
  }
}
